package cn.hzxs.zjn.studentmanagement.dao;

import cn.hzxs.zjn.studentmanagement.domain.Page;
import cn.hzxs.zjn.studentmanagement.domain.Task;
import cn.hzxs.zjn.studentmanagement.domain.User;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Repository;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @Author: Zjn *Bring it on!!!*
 * @Date: 2021/3/28 13:51
 * Note:
 */
@Repository
public interface TaskMapper {
    @Results(id = "TaskMap", value = {
            @Result(column = "task_id", property = "TASKID", javaType = Integer.class),
            @Result(column = "task_title", property = "TASKTITLE", javaType = String.class),
            @Result(column = "task_detail", property = "TASKDETAIL", javaType = String.class),
            @Result(column = "task_lables", property = "TASKLABLES", javaType = String.class),
            @Result(column = "progress", property = "PROGRESS", javaType = Integer.class),
            @Result(column = "author_id", property = "AUTHORID", javaType = Integer.class),
            @Result(column = "create_time", property = "CREATETIME", javaType = String.class)
    })

    @Select("select task_id,task_title,task_detail,task_lables,progress,author_id,user.user_icon,create_time " +
            " from task,user where author_id=user.user_id" +
            " limit #{page.OFFSET},#{page.LIMIT};")
    List<Task> getTask(@Param("page") Page page);   /*获取任务*/

    @Select("select u.user_id as userId,u.user_name as userName,u.user_icon as userIcon from user u,usertask k where" +
            " k.task_id=#{taskid} and k.user_id=u.user_id;")
//    @ResultMap("UserMap")
    List<Map> getUserByTask(Integer taskid);


    @Insert("insert into task (task_title,task_detail,task_lables,author_id,create_time,update_time)" +
            " values (#{task.TASKTITLE},#{task.TASKDETAIL},#{task.TASKLABLES}," +
            "#{task.AUTHORID},current_date(),current_date())")
    void addTask(@Param("task") Task task);     /*添加任务*/

    @Select("select count(task_title) from task where task_title=#{task.TASKTITLE}")
    boolean ifExistTitle(@Param("task") Task task);

    @Insert("insert into usertask (user_id,task_id) values" +
            " (#{userId},#{task.TASKID});")
    void insertPeopleByTask(@Param("task") Task task, Integer userId);

    @Select("select task_id from task where " +
            " task_title=#{task.TASKTITLE}")
    Integer selectTaskIdBy(@Param("task") Task task);


    @Delete("delete from task where task_id=#{task.TASKID};")
    void deleteTask(@Param("task") Task task);  /*删除任务*/

    @Update("update task set progress=#{task.PROGRESS}" +
            " where task_id=#{task.TASKID};")
    void updateTask(@Param("task") Task task);  /*更新任务进度*/
}
